package store.lunangangster.engine.impl.service;

import store.lunangangster.engine.ProcessEngineConfiguration;
import store.lunangangster.engine.impl.cmd.Command;
import store.lunangangster.engine.impl.cmd.HistoryDeleteCmd;
import store.lunangangster.engine.impl.entity.History;
import store.lunangangster.engine.impl.interceptor.CommandContext;
import store.lunangangster.engine.impl.query.HistoryQuery;
import store.lunangangster.engine.impl.query.HistoryQueryImpl;
import store.lunangangster.engine.service.HistoryService;

public class HistoryServiceImpl extends ServiceImpl implements HistoryService {

  public HistoryServiceImpl(
      ProcessEngineConfiguration processEngineConfiguration) {
    super(processEngineConfiguration);
  }

  @Override
  public HistoryQuery createHistoryQuery() {
    return executor.execute(new Command<HistoryQuery>() {
      @Override
      public HistoryQuery execute(CommandContext commandContext) {
        return new HistoryQueryImpl(executor);
      }
    });
  }

  @Override
  public void deleteHistory(String historyId) {
    executor.execute(new HistoryDeleteCmd(historyId, null, null));
  }

  @Override
  public void deleteHistory(History history) {
    deleteHistory(history.getId());
  }

  @Override
  public void deleteHistoryByProcessInstanceId(String processInstanceId) {
    executor.execute(new HistoryDeleteCmd(null, processInstanceId, null));
  }

  @Override
  public void deleteHistoryByProcessInstanceId(History history) {
    deleteHistoryByProcessInstanceId(history.getProcessInstanceId());
  }

  @Override
  public void deleteHistoryByProcessInstanceName(String processInstanceName) {
    executor.execute(new HistoryDeleteCmd(processInstanceName, null));
  }

  @Override
  public void deleteHistoryByProcessInstanceName(History history) {
    deleteHistoryByProcessInstanceName(history.getProcessInstanceName());
  }

  @Override
  public void deleteHistoryByProcessDefinitionId(String processDefinitionId) {
    executor.execute(new HistoryDeleteCmd(null, null, processDefinitionId));
  }

  @Override
  public void deleteHistoryByProcessDefinitionId(History history) {
    deleteHistoryByProcessDefinitionId(history.getProcessDefinitionId());
  }

  @Override
  public void deleteHistoryByProcessDefinitionName(String processDefinitionName) {
    executor.execute(new HistoryDeleteCmd(null, processDefinitionName));
  }

  @Override
  public void deleteHistoryByProcessDefinitionName(History history) {
    deleteHistoryByProcessDefinitionName(history.getProcessDefinitionKey());
  }
}
